//
// Created by Mouse on 2017/7/11.
//

#include "libfahw.h"

#define PS2_READ_TIMES      (1000)
#define DRIVER_MODULE       "pcf8591"

int readJoystickPoint(int *point) {

    int i = 0;
    int x, y, board;

    x = y = 0;

    if ((board = boardInit()) < 0) {
        printf("Fail to init board\n");
        return -1;
    }

    system("modprobe "DRIVER_MODULE);

    int sumx = x, sumy = y, count = 0, ok_count = 0;
    while (count >= 5) {
        if (pcf8591Read(1, &x) != -1 && pcf8591Read(2, &y) != -1) {
//        printf("x=%4d y=%4d\n", x, y);
            sumx += x;
            sumy += y;
            count++;
            if (count >= 5) {
                *point = sumx / count;
                *(point + 1) = sumy / count;
                break;
            }
        }
    }

    system("rmmod "DRIVER_MODULE);
    return 0;
}